package com.lanhao.aot.carrierintegration.anjun;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import cn.hutool.http.HttpRequest;

/**
  * 安骏物流  -  对接巴西邮政
 * @author kunzhou3
 *
 */
public class AnJunUtil {
	private static final Logger LOG = LoggerFactory.getLogger(AnJunUtil.class);
	public static final String ORDER_URL = "https://takeno.bailidaming.com/logistics/order/api/create";
	public static final String TRACKING_URL = "https://expand.bailidaming.com/expandApi/open/reverse/logistics/track";
	public static final String CustomerChannelId = "1907";
	public static final String TEST_TOKEN = "dRj3HEUrickx8/B15m0IoAiwicH9c4tp5tUuMy7M93lF5dqDqocpzYR+8a0fNDLDNctahmtces12sjKyjv/BsAh5rdbauvXSopFfrJzOWvLu+xwgb10WfQUx+5bl6zOuNTi+hnhdrdilLpXTInG7ZCrHE7CkGFxXG31WxhEwvb6fKNCWg5b3msYhDHoF1WwtlygkryCYf8A4E4eUZ2WDt40+N7JcISVpC+dMqHrtIgpIxWUq9eBoZ8gcOsQ+a/mm75tSuruC4eXCErOQaH7drMcAkIOvmd4vDnB39Pli2bDdytND8K2KBINXgwl5CjM/XWb+1hNZv0AXXDpZ0PZc/Q==,ZRaWOy7rqj6JHkI9YDb7QDVgL0hFgK6jn1IStVQe7Kz2/XVk5xPmXgpmzNpBj4M5lVcfuGWG06j4cIuwXlzjc5F/4WSrHi3ZBOe8jM/pILbAShi5xAv5UOAPNNlRxeb6ohSyCvw8akMeaO0sWRvNxwBd3LuQwnR0HxbAQZxextY=,";
	
	public static final String PROD_TOKEN = "Jt3c2gRJAUlAiENZBb+6WkoVshElaG6MXmcaxTraKisOpxqMn5IU+bxnjGW+e7wwuVKyHpUOEMG+dUyp+aYiRzVNns2ltlmaCvK4albbOXGNFRzM5xasBYU9NPAdGyx9oXcFHLK8m0BVstJX/Kn986KPAYEYkPefc+hUSkScb8w=,exmd9UjXwFn5oHW4rJQjtAx8vc3aFtrXxb7KdwCj2Gg+xSc1bt+T9CGaAsPADC5SyRaUKVxE8L8BtTIugl9zWgc03lDYNUYyU5VI85jfpZD3mJLqs+fvWlJfgOOV2tVYPmiCrVXvnsV8TYcDUQ8Y/ffhk9TMZ7eTx/se3DTwZ5E=,";
	
	public static void main(String[] args) {
//		createOrder();
		trackingOrder("2710945495");
	}
	
	public static void createOrder() {
		JSONObject request = new JSONObject();
		request.put("customerChannelId", CustomerChannelId);
		request.put("orderType", "1");
		request.put("currency", "USD");
		request.put("orderNumber", "202307190001test-fortest003");
		request.put("hasBack", "0");
		request.put("packageType", "goods");
		request.put("prepaymentVat", "other");
//		request.put("deliveryTerms", "");
//		request.put("vat", "");
//		request.put("totalDeclaredWeight", 0.3);
//		request.put("totalDeclaredValue", 0.01);
		
		JSONArray invoiceInfo = new JSONArray();
		JSONObject invoice = new JSONObject();
		invoice.put("quantity", 1);
		invoice.put("company", "");
		invoice.put("declaredValue", 0.3);
		invoice.put("declaredWeight", 0.01);
		invoice.put("nameCn", "Espace货物");
		invoice.put("nameEn", "Espace Goods");
		invoice.put("hscode", "64021900");
//		invoice.put("sku", "");
//		invoice.put("purpose", "");
//		invoice.put("material", "");
//		invoice.put("placeOrigin", "CN");
//		invoice.put("brand", "OleB");
//		invoice.put("length", 10);
//		invoice.put("width", 10);
//		invoice.put("height", 10);
//		invoice.put("specs", "");
//		invoice.put("caseNumber", "");
//		invoice.put("distributionInfo", "CAP");
//		invoice.put("goodsUrl", "https://pt.aliexpress.com/");
//		invoice.put("picturesUrl", "https://pt.aliexpress.com/");
//		invoice.put("salesPlatform", "");
		invoice.put("powder", 0);
		invoice.put("liquid", 0);
		invoice.put("hasBattery", 0);
//		invoice.put("batteryType", "");
		invoice.put("magnetic", 0);
		invoiceInfo.add(invoice);
		request.put("invoiceInfo", invoiceInfo);
		
//		request.put("insuranceFee", 0.01);
//		request.put("freightFee", 0.01);
		
		JSONObject receiverInfo = new JSONObject();
		receiverInfo.put("receiveName", "Loren Araos Teran");
		receiverInfo.put("receivePhone", "83996112463");
		receiverInfo.put("receiveMobile", "");
		receiverInfo.put("receiveMail", "");
		receiverInfo.put("receiveCountry", "BR");
		receiverInfo.put("receiveProvince", "PB");
		receiverInfo.put("receiveCity", "Campina Grande");
		receiverInfo.put("receiveArea", "");
		receiverInfo.put("receiveStreet", "");
		receiverInfo.put("receiveHouseNumber", "");
		receiverInfo.put("receiveAddress", "José Pedro Fernandes 328");
		receiverInfo.put("receiveZipcode", "58400002");
		receiverInfo.put("receiveCompany", "");
		receiverInfo.put("receiveTax", "");
		receiverInfo.put("receiveCertificateType", "");
		receiverInfo.put("receiveCertificateCode", "");
		request.put("receiverInfo", receiverInfo);
		
		JSONObject senderInfo = new JSONObject();
		senderInfo.put("senderName", "Alan_Boy");
		senderInfo.put("senderPhone", "11970796992");
		senderInfo.put("senderMobile", "");
		senderInfo.put("senderCountry", "BR");
		senderInfo.put("senderProvince", "PB");
		senderInfo.put("senderCity", "Campina Grande");
		senderInfo.put("senderMail", "");
		senderInfo.put("senderArea", "");
		senderInfo.put("senderStreet", "");
		senderInfo.put("senderHouseNumber", "");
		senderInfo.put("senderAddress", "José Pedro Fernandes 329");
		senderInfo.put("senderZipcode", "58400002");
		senderInfo.put("senderCompany", "");
		senderInfo.put("senderTax", "");
		senderInfo.put("senderCertificateType", "");
		senderInfo.put("senderCertificateCode", "");
		request.put("senderInfo", senderInfo);
		
//		JSONObject collectionReturnParams = new JSONObject();
//		collectionReturnParams.put("collectionReturnName","Alan_Boy");
//		collectionReturnParams.put("collectionReturnPhone", "18928948808");
//		collectionReturnParams.put("collectionReturnCountry", "BR");
//		collectionReturnParams.put("collectionReturnProvince", "PR");
//		collectionReturnParams.put("collectionReturnCity", "BRAS");
//		collectionReturnParams.put("collectionReturnZipcode", "09900001");
//		collectionReturnParams.put("collectionReturnAddress", "BURONG LONG ROAD 1892");
//		collectionReturnParams.put("collectionReturnEmail", "it@szanjun.com");
//		collectionReturnParams.put("collectionReturnTax", "");
//		collectionReturnParams.put("collectionReturnMobile", "");
//		collectionReturnParams.put("collectionReturnArea", "CARAPICUIBA");
//		collectionReturnParams.put("collectionReturnStreet", "");
//		collectionReturnParams.put("collectionReturnHouseNumber", "");
//		collectionReturnParams.put("collectionReturnCompany", "");
//		collectionReturnParams.put("collectionReturnCertificateType", "");
//		collectionReturnParams.put("collectionReturnCertificateNumber", "");
//		request.put("collectionReturnParams", collectionReturnParams);
		
		Map<String, String> headers = getAuthHeader();
		
		String respBody = HttpRequest.post(ORDER_URL).addHeaders(headers).body(request.toJSONString()).execute().body();
		LOG.info("调用下单接口request={}, 返回结果resp={}", request, respBody);
		
		// {"code":200,"msg":"success","data":{"orderNum":"202307190001test-fortest002","systemNum":"BYT101509397BR","trackNum":"2578590320","labelUrl":"https://anjunggl-1313947203.cos.ap-guangzhou.myqcloud.com/upload/LOGISTICS_CHANNEL_LABEL/BRAZIL_POST_REVERSE_DELIVERY/2023-07-20/2578590320.pdf?q-sign-algorithm=sha1&q-ak=AKID2vc8UcWIfqskPtEKDtMfookXDD1e6JP6&q-sign-time=1689833920%3B1721319393&q-key-time=1689833920%3B1721319393&q-header-list=host&q-url-param-list=country&q-signature=8e6bb2fdb52e1964b3a37802717e8bcbbe1da910&country=cn","date":"2023-07-20 14:18:40","expandParams":null},"extra":""}
	}
	
	public static Map<String, String> getAuthHeader() {
		Map<String, String> headers = new HashMap<String, String>();
		headers.put("Authorization", PROD_TOKEN);
		return headers;
	}
	
	public static String trackingOrder(String orderNo) {
		Map<String, String> headers = getAuthHeader();
		JSONObject request = new JSONObject();
		request.put("orderNo", orderNo);
		String respBody = HttpRequest.post(TRACKING_URL).addHeaders(headers).body(request.toJSONString()).execute().body();
		LOG.info("调用订单跟踪接口request={}, 返回结果resp={}", request, respBody);
		// {"code":200,"msg":"success","data":{"trackNo":"QC548096995BR"},"extra":""}
		// {"code":200,"msg":"success","data":{"trackNo":""},"extra":""}
		return respBody;
	}
	
}
